package com.learn.exec;

/**
 * 创建上卖票窗口，总票数100张
 */
class Window extends Thread {
    private static int ticket = 100;
    private int sale = 0;

    @Override
    public void run() {
        while (true) {
            if (ticket > 0) {
                System.out.println(getName() + "售票:票号为" + ticket);
                ticket--;
                sale++;
            } else {
                break;
            }
        }
        System.out.println(getName()+ "售出" +sale + "张票");
    }
}

class RunnableWindow implements Runnable {
    private int ticket = 100;
    private int sale = 0;

    @Override
    public void run() {
        while (true) {
            if (ticket > 0) {
                System.out.println(Thread.currentThread().getName() + "售票:票号为" + ticket);
                ticket--;
                sale++;
            } else {
                break;
            }
        }
        System.out.println(Thread.currentThread().getName()+ "售出" +sale + "张票");
    }
}

public class Ticket {
    public static void main(String[] args) {
        RunnableWindow window = new RunnableWindow();
        Thread w1 = new Thread(window);
        Thread w2 = new Thread(window);
        Thread w3 = new Thread(window);

        w1.setName("窗口1");
        w1.setPriority(Thread.MAX_PRIORITY);
        w2.setName("窗口2");
        w2.setPriority(Thread.MIN_PRIORITY);
        w3.setName("窗口3");
        w3.setPriority(Thread.NORM_PRIORITY);

        w1.start();
        w2.start();
        w3.start();
    }
}
